import { getIabCategories, getCampaignsList, getTelephoneCode } from "@/api/pulldown-data";
import { countries } from '../../assets/JSdata/campaignOptions'

const publicData = {
    state: {
        iabCategories: null,
        countries: null,
        campaignList: null,
        telephoneCodeOptions: null
    },
    mutations: {
        changeIabCategories(state, newIabCategories) {
            if (newIabCategories) {
                localStorage.setItem('iabCategories', JSON.stringify(newIabCategories));
                state.iabCategories = newIabCategories
            }

        },
        changeCountries(state, newCountries) {
            if (newCountries) {
                localStorage.setItem("countries", JSON.stringify(newCountries));
                state.countries = newCountries
            }
        },
        changeCampaignList(state, newCampaignList) {
            if (newCampaignList) {
                localStorage.setItem("campaignList", JSON.stringify(newCampaignList));
                state.campaignList = newCampaignList
            }
        },
        changeTelephoneCodeOptions(state, newArray) {
            if (newArray) {
                localStorage.setItem("telephoneCodeOptions", JSON.stringify(newArray));
                state.telephoneCodeOptions = newArray
            }
        }
    },
    actions: {
        //黑白名单类别
        GetIabCategories({ commit }) {
            return new Promise((resolve, reject) => {
                let data = JSON.parse(localStorage.getItem('iabCategories'));
                if (data != null) {
                    resolve(data)
                } else {
                    getIabCategories().then(response => {
                        const result = response.data.data;
                        commit("changeIabCategories", result);
                        resolve(result)
                    }).catch(error => {
                        reject(error)
                    })
                }

            })
        },
        //国家
        GetCountries({ commit }) {
            return new Promise((resolve, reject) => {
                let data = JSON.parse(localStorage.getItem('countries'));
                if (data && Array.isArray(data) && data.length >= 0) {
                    resolve(data)
                } else {
                    commit("changeCountries", countries);
                    resolve(countries);
                }
            })
        },

        //电话
        getTelephoneCodeOptions({ commit }) {
            return new Promise((resolve, reject) => {
                let data = JSON.parse(localStorage.getItem('telephoneCodeOptions'));

                if (data && Array.isArray(data) && data.length >= 0) {
                    resolve(data)
                } else {
                    getTelephoneCode().then(res => {
                        let { data } = res.data || [];
                        commit("changeTelephoneCodeOptions", data);
                        resolve(data)
                    }).catch(e => {
                        reject(e)
                    })
                }
            })
        },

        //广告活动
        GetCampaignList({ commit }) {
            return new Promise((resolve, reject) => {
                getCampaignsList('campaigns').then(response => {
                    const result = response.data.data;
                    commit("changeCampaignList", result);
                    resolve(result)
                }).catch(error => {
                    reject(error)
                })
            })
        },
    }
}
export default publicData
